package tbg;

import net.tt.util.CloseUtil;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * F:\\Users\\tbc\\Desktop\\ll.m3u
 *
 * @author tbc on 2016/11/26 22:13.
 */
public class M3uProcess {
    public static void main(String[] args) throws InterruptedException, IOException {
        System.out.println("拖入m3u文件[系统会将同目录下所有文件（注：所有文件）的全文件名写入m3u文件中]:");
        Scanner s = new Scanner(System.in);
        String m3uFile = s.nextLine();
        if (m3uFile == null || "".equals(m3uFile)) {
            int djs = 3;
            System.out.println("未接收到文件目录，系统将退出");
            for (int i = 0; i < djs; i++) {
                System.out.println(djs - i);
                TimeUnit.SECONDS.sleep(1);
            }
            System.exit(0);
        }
        String[] ss = m3uFile.split("\\\\");
        String m3u = Arrays.stream(ss).filter(it -> it.endsWith(".m3u")).findFirst().orElse("");
        String path = Arrays.stream(ss)
                .filter(str -> !str.endsWith(".m3u"))
                .collect(Collectors.joining("\\"));

//        File directory = ;
        File[] fileList = new File(path).listFiles();
        assert fileList != null;
        List<String> files = Arrays.stream(fileList)
                .filter(f -> !f.isDirectory())
                .map(File::getName)
                .filter(f -> !f.endsWith(".m3u"))
                .collect(Collectors.toList());

        OutputStreamWriter os = new FileWriter(m3uFile, true);
        files.forEach(f -> {
            try {
                os.write(f + "\n");
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        CloseUtil.close(os);
    }
}
